package com.xindaoapp.happypet.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.xindaoapp.happypet.R;

public class ProgressHUD extends Dialog {
    public ProgressHUD(Context context) {
        super(context);
    }

    /**
     * 使用该构造方法设置主题为透明
     * 
     * @param context
     * @param theme
     */
    public ProgressHUD(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
        AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
        spinner.start();
    }

    /**
     * 设置dialog消息
     * 
     * @param message
     */
    public void setMessage(CharSequence message) {
        if (message != null && message.length() > 0) {
            findViewById(R.id.message).setVisibility(View.VISIBLE);
            TextView txt = (TextView) findViewById(R.id.message);
            txt.setText(message);
            txt.invalidate();
        }
    }

    /**
     * 变更为播放动画
     * 
     * @param context
     * @param message
     * @param indeterminate
     * @param cancelable
     * @param cancelListener
     * @return
     */
    public static ProgressHUD show(Context context, CharSequence message, boolean indeterminate,
            boolean cancelable, OnCancelListener cancelListener) {
        // 初始化dialog 加载透明样式
        ProgressHUD dialog = new ProgressHUD(context, R.style.ProgressHUD);
        // dialog.setTitle("");
        // 加载自定义布局
        dialog.setContentView(getView(context));

        if (message == null || message.length() == 0) {
            dialog.findViewById(R.id.message).setVisibility(View.GONE);
        } else {
            TextView txt = (TextView) dialog.findViewById(R.id.message);
            txt.setText(message);
        }

        // 设置点击空白区域可以隐藏dialog
        dialog.setCancelable(cancelable);
        // dialog隐藏时的回调
        dialog.setOnCancelListener(cancelListener);
        // dialog窗口居中显示
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.dimAmount = 0.2f;
        dialog.getWindow().setAttributes(lp);
        try {
            // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            dialog.show();
        } catch (Exception e) {
        }
        return dialog;
    }

    public static View getView(Context context) {
        // 加载自定义显示布局
        View view = View.inflate(context, R.layout.progress_hud, null);
        ImageView spinnerImageView = (ImageView) view.findViewById(R.id.spinnerImageView);
        // 给布局添加动画
        // spinnerImageView.setBackgroundResource(R.anim.alertdialog_animation);
        spinnerImageView.setBackgroundResource(R.anim.spinner);
        // 获取动画
        final AnimationDrawable animation = (AnimationDrawable) spinnerImageView.getBackground();
        // 设置动画执行多次
        animation.setOneShot(false);
        new Runnable() {
            @Override
            public void run() {
                // 开启动画
                animation.start();
            }
        };
        return view;
    }
}
